#include "ConfigReader.h"
#include "FileAccess.h"
#include "FileUtils.h"
#include <unistd.h>
#include <iostream>
#include <string.h>
#include "http-config.h"
namespace controller {
    void FileAccessController::service(HttpRequest &request, HttpResponse &response) {
        std::string filePath = configReader.getConfig(CONFIG_WEB_STATIC_DIR, CONFIG_WEB_STATIC_DIR_DEFAULT);
        // 过滤掉"../.."防止越过站点根目录
        Path uriPath;
        uriPath.append(request.getURI().c_str());
        filePath.append(uriPath.getPath());
        try {
            FileInfo fi = FileInfo::getFromLocal(filePath);
            if(fi.isDir()) filePath += "/index.html";
            response.sendFile(request, filePath);
        } catch(const FileException &e) {
            WebRouteMapper::getErrorController(404)
                        ->service(request, response);
        } catch(const std::exception& e) {
            std::cerr << e.what() << '\n';
        }
        
    }
}